/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package explorer;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

/**
 *
 * @author user1
 */
public final class FileUnited {
    private String filePath;
    private String fileUri;
    private String fileContent;
    int separator, pathSeparator;
    
    public List<Integer> startChar = new ArrayList<Integer>();
    public List<Integer> tokenLength = new ArrayList<Integer>();
    
    public List<Integer> startJudul = new ArrayList<Integer>();
    public List<Integer> tokenJudul = new ArrayList<Integer>();
    
    public FileUnited(File f) {
        filePath = f.getAbsolutePath();
        separator = filePath.lastIndexOf('.');
        pathSeparator = filePath.lastIndexOf(File.separatorChar);
        fileUri = f.toURI().toString();
        if (f.getName().toUpperCase().endsWith("DOCX")) {
            try {
                FileInputStream fis = new FileInputStream(f);
                XWPFDocument doc = new XWPFDocument(fis);
                XWPFWordExtractor ex = new XWPFWordExtractor(doc);
                fileContent = ex.getText();
            } catch (Exception e1) {
            }
        } else if (f.getName().toUpperCase().endsWith("DOC")) {
            try {
                FileInputStream fis = new FileInputStream(f);
                HWPFDocument doc = new HWPFDocument(fis);
                WordExtractor we = new WordExtractor(doc);
                fileContent = we.getText();
            } catch (Exception e1) {
            }
        } else {
            try {
                setContent(new FileReader(f));
            } catch (IOException e) {
                fileContent = e.toString();
            }
        }
    }
    
    public FileUnited(FileUnited fu) {
        filePath = fu.getFilePath();
        fileUri = fu.getUri();
        separator = filePath.lastIndexOf('.');
        fileContent = fu.getContent();
        pathSeparator = filePath.lastIndexOf(File.separatorChar);
    }
    
    public void setStartChar(int i) {
        startChar.add(i);
    }
    
    public void setTokenLength(int i) {
        tokenLength.add(i);
    }
    
    public void setTokenJudul(int i) {
        tokenJudul.add(i);
    }
    
    public void setStartJudul(int i) {
        startJudul.add(i);
    }
    
    public String getUri() {
        return fileUri;
    }
    
    public String getFilePath() {
        return filePath;
    }
    
    public String getExtension() {
        String result = filePath.substring(separator+1);
        if (result.isEmpty()) return "";
        else return result;
    }
    
    public String getName() {
        return filePath.substring(pathSeparator+1,separator);
    }
    
    public String getPath() {
        return filePath.substring(0,separator);
    }
    
    public String getFullPath() { 
        //return fileUri;
        return new StringBuffer(fileUri).insert(fileUri.indexOf('/'),"//").toString();
    }
    
    public String getContent() {
        return fileContent;
    }
    
    private void setContent(Reader f) throws IOException{
        BufferedReader br = new BufferedReader(f);
        StringBuilder result = new StringBuilder();
        String aLine;
        while ((aLine = br.readLine()) != null)
            result.append(aLine);
        fileContent = result.toString();
    }
    
    public static boolean fileValidator(File f) {
        String upperName = f.getName().toUpperCase();
        return (
                upperName.endsWith("TXT") ||
                upperName.endsWith("DOC") ||
                upperName.endsWith("HTML") ||
                upperName.endsWith("PAS") ||
                upperName.endsWith("C") ||
                upperName.endsWith("JSP") ||
                upperName.endsWith("CPP") ||
                upperName.endsWith("CS") ||
                upperName.endsWith("JAVA") ||
                upperName.endsWith("PHP") ||
                upperName.endsWith("DOCX")
                );
    }
    
    public void sorting() {
        int min = 0;
        int temp;
        for (int i = 0; i < startChar.size()-1; i++) {
            min = i;
            for (int j = i+1; j < startChar.size(); j++) {
                if (startChar.get(min) > startChar.get(j)) {
                    min = j;
                }
            }
            temp = startChar.get(i);
            startChar.set(i, startChar.get(min));
            startChar.set(min, temp);
            temp = tokenLength.get(i);
            tokenLength.set(i,tokenLength.get(min));
            tokenLength.set(min, temp);
        }
    }
    
    public void sortingJudul() {
        int min = 0;
        int temp;
        for (int i = 0; i < startJudul.size()-1; i++) {
            min = i;
            for (int j = i+1; j < startJudul.size(); j++) {
                if (startJudul.get(min) > startJudul.get(j)) {
                    min = j;
                }
            }
            temp = startJudul.get(i);
            startJudul.set(i, startJudul.get(min));
            startJudul.set(min, temp);
            temp = tokenJudul.get(i);
            tokenJudul.set(i,tokenJudul.get(min));
            tokenJudul.set(min, temp);
        }
    }
}
